#pragma once
#include "windows.h"
#include "Object.h"
#include "txComPtr.h"
#include "Event.h"
#include "Decode.h"
#include <list>
#include <vector>

template<typename T>
bool CreateObject(T **obj)
{
	if (obj) {
		(*obj) = new T;
		(*obj)->addref();
	}
	return true;
}

class View : public Object
{
public:
	typedef EmptyType Delegater;
	typedef void (Delegater::*OnClickFunc)(const POINT* pt);
	typedef void (Delegater::*OnMouseEnterFunc)();
	typedef void (Delegater::*OnMouseLeaveFunc)();
	typedef void (Delegater::*OnInvalidateRectFunc)(const RECT *lpRect);
	typedef void (Delegater::*OnSizeChangedFunc)(const SIZE* pSize);

public:
	View();
	virtual ~View();

public:
	bool Create(View *pParent);
	bool Destroy();
	bool IsValid();
	void AddChild(View* pObj);
	void GetParent(View** ppObj);
	void SetSize(const SIZE * pSize);
	void SetPos(const POINT * pPos);
	void SetToolTips(const wchar_t *pTips);
	void GetPos(POINT *pPos);
	void GetRect(RECT *pRect);
	void GetSize(SIZE *pSize);
	void SetTexture(const wchar_t * pNormal, const wchar_t *pHighlight, const wchar_t *pMouseDown);
	void Set9Gird(const RECT *lpRect);

	virtual void Render(HDC hdc, const RECT *lpRect);
	void DoClick(const POINT* pt);
	
	void DoSizeChange(const SIZE *pSize);
	void DoMouseMove(const POINT* pt);
	void DoMouseEnter();
	void DoMouseLeave();
	void DoMouseLeftDown(const POINT* pt);
	void DoMouseLeftUp(const POINT* pt);

	void DoTimer(void *id, bool & bFind);
	void InvalidateRect(const RECT *lpRect);

protected:
	virtual void Click(const POINT* pt);
	virtual void MouseEnter();
	virtual void MouseLeave();
	virtual void Timer(void *id);

public:
	Event<OnClickFunc> OnClick;
	Event<OnMouseEnterFunc> OnMouseEnter;
	Event<OnMouseLeaveFunc> OnMouseLeave;
	Event<OnInvalidateRectFunc> OnInvalidateRect;
	Event<OnSizeChangedFunc> OnSizeChanged;

public:
	void ChildHitTest(const POINT *lpPoint, View** ppView);

private:

protected:
	typedef std::list<TXCAdapt< TXCComPtr<View> > > ViewLst;
	
	TXCComPtr<View> m_pParent;
	ViewLst m_vecChild;
	SIZE m_sz;
	POINT m_location;
	float m_alpha;
	bool m_bHover;
	DWORD m_dwLstCount;
	RECT m_rc9Gird;
	bool m_bMouseDown;
	bool m_bMouseUpping;
	bool m_bMouseLeaving;
	bool m_bTransparent;
	bool m_bValid;
	HBITMAP m_hNormal, m_hHighlight, m_hMouseDown;
};